package com.hhl.flower.controller;

import com.hhl.flower.common.result.Result;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.pojo.entity.Coupon;
import com.hhl.flower.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/coupon")
@Api(tags = "用户优惠卷相关接口")
@Slf4j
public class CouponController {
	@Autowired
	private CouponService couponService;

	@GetMapping("/list")
	@ApiOperation("查询用户所有优惠卷")
	public Result<List<Coupon>> list(){
		Integer currentId = BaseContext.getCurrentId();
		List<Coupon> couponList = couponService.getListByUser(currentId);

		return Result.success(couponList);
	}

	@GetMapping("/list/by/status")
	@ApiOperation("根据状态查询优惠券")
	public Result<List<Coupon>> listByStatus(@RequestParam("status") Integer status){
		Integer currentId = BaseContext.getCurrentId();
		List<Coupon> couponList = couponService.getListByStatus(currentId, status);
		return Result.success(couponList);
	}

	@GetMapping("/count")
	@ApiOperation("查询可用优惠券数量")
	public Result<Integer> getCount(){
		Integer currentId = BaseContext.getCurrentId();
		return Result.success(couponService.getCouponCount(currentId));
	}

	@PostMapping("/get")
	@ApiOperation("获取优惠券")
	public Result getCoupon(@RequestParam("discount") Double discount, @RequestParam("threshold") Double threshold, @RequestParam("termTime") Integer termTime) {
		Integer currentId = BaseContext.getCurrentId();
		couponService.addCoupon(currentId, discount, threshold, termTime);
		return Result.success();
	}

}
